﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2/27/2011
 * Time: 2:19 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using TomatoSynchronizer.Entities;
using TomatoSynchronizer.Store;
using HtmlAgilityPack;
using System.Windows.Forms;

namespace TomatoSynchronizer.Forms
{
	/// <summary>
	/// Description of CategorySyncForm.
	/// </summary>
	public class CategorySyncForm : Form
	{
		private Site _siteSrc;
		private Site _siteDest;
		private HtmlWeb _client;
		private CategoryStore categorieStoreSrc;
		private CategoryStore categorieStoreDest;
		private Category[] categoriesSrc;
		private Category[] categoriesDest;
		
		public CategorySyncForm(Site siteSrc,Site siteDest,HtmlWeb client)
		{
			_client = client;
			_siteSrc = siteSrc;
			_siteDest = siteDest;
			
			categorieStoreSrc = new CategoryStore(_siteSrc,_client);
			categorieStoreDest = new CategoryStore(_siteDest,_client);
			Show();
		}
		
		private void UpLoadItems(Category[] categoriesSrc,Category[] categoriesDest)
		{
			MappingStore mappingStore = new MappingStore(_siteSrc,_siteDest,"Categories");
			
			foreach(Category categorySrc in categoriesSrc)
			{
				Category categoryDest = categorieStoreDest.getCategoryByName(categorySrc.categories_name);
				if(categoryDest != null)
				{
					if(!categorySrc.Equals(categoryDest))
					{
						//update src
					}
					
					Mapping mapping= new Mapping(categorySrc.categories_id,categoryDest.categories_id,"categories_id");
					mappingStore.Mappings.Add(mapping);
				}
				else
				{
					//insert src
					string remoteId = string.Empty;
					
					if (categorySrc.parent_id == "0")
					{
						UploadItem(categorySrc,"0",mappingStore);
					}
				}
			}
			
			mappingStore.Save();
		}
		
		public void UploadItem(Category category, string parentId,MappingStore mappingStore)
		{
			string remoteId = string.Empty;
			
			remoteId = categorieStoreDest.InsertCategory(category,parentId);
			
			if(remoteId != string.Empty)
			{
				foreach (Category categorie in categoriesSrc)
				{
					if (categorie.parent_id == category.categories_id)
					{
						Category categoryDest = categorieStoreDest.getCategoryByName(categorie.categories_name);
						if(categoryDest != null)
						{
							if(!categorie.Equals(categoryDest))
							{
								//update src
							}
							
							Mapping mapping= new Mapping(categorie.categories_id,categoryDest.categories_id,"categories_id");
							mappingStore.Mappings.Add(mapping);
						}
						else
						{
							UploadItem(categorie,remoteId,mappingStore);
							
							Mapping mapping= new Mapping(category.categories_id,remoteId,"categories_id");
							mappingStore.Mappings.Add(mapping);
						}
					}
				}
			}
			else
			{
				MessageBox.Show("unable to insert the category " + category.categories_name +  " in site " + mappingStore.SiteDest.Name);
			}
		}
		
		public void Run()
		{
			categoriesSrc =  categorieStoreSrc.LoadItems();
			
			categoriesDest =  categorieStoreDest.LoadItems();
			
			//pas bon
			UpLoadItems(categoriesSrc,categoriesDest);
			UpLoadItems(categoriesDest,categoriesSrc);
			
			Close();
		}
	}
}
